package cookie.swing;

import cookie.Main;
import cookie.lib.XMLParsing;
import cookie.wrappers.Input;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;

/**
 * Created by IntelliJ IDEA.
 * User: Nathaniel
 * Date: 12/10/11
 * Time: 3:30 PM
 */

public class CheckGUI extends JFrame {
    private ArrayList<Input> questions = new ArrayList<Input>();

    private File file;
    
    private int total;
    private int good = 0;
    private int errors = 0;
    
    private int current = 0;

    public CheckGUI(File file) {
        this.file = file;

        Element element = XMLParsing.getDocument(file).getDocumentElement();
        NodeList node = element.getElementsByTagName("input");
        if (node != null && node.getLength() > 0) {
            for (int i = 0; i < node.getLength(); i++) {
                Element temp = (Element) node.item(i);
                questions.add(getInput(temp));
            }
        }
        
        total = questions.size();

        questionField = new JTextField();
        answerField = new JTextField();
        questionLabel = new JLabel();
        answerLabel = new JLabel();
        submitBtn = new JButton();

        setTitle("Apex Homework - Check list");
        Container contentPane = getContentPane();

        questionField.setEditable(false);
        questionField.setText(questions.get(current).getQuestion());

        questionLabel.setText("Question:");

        answerLabel.setText("Answer:");

        submitBtn.setText("Submit answer");
        submitBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    if(answerField.getText().equals(questions.get(current).getAnswer())){
                        good++;
                    } else {
                        errors++;
                    }
                    current++;
                    if(current >= total) {
                        JOptionPane.showMessageDialog(null, "You've completed the list.\nYou had " + errors + " errors.\n\nScore: " + (int) (((double) good / (double) total) * 100) + "%");
                        Main m = new Main();
                        m.setVisible(true);
                        dispose();
                    } else {
                        questionField.setText(questions.get(current).getQuestion());
                        answerField.setText("");
                    }
                } catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(answerField, GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                                        .addComponent(questionLabel)
                                        .addComponent(answerLabel)
                                        .addComponent(questionField, GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                                        .addComponent(submitBtn, GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE))
                                .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(questionLabel)
                                .addGap(5, 5, 5)
                                .addComponent(questionField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(answerLabel)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(answerField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(submitBtn)
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }
    private JTextField questionField;
    private JTextField answerField;
    private JLabel questionLabel;
    private JLabel answerLabel;
    private JButton submitBtn;
    
    public Input getInput(Element element){
        if(element != null){
            try{
                String question = XMLParsing.getValue(element, "question");
                String answer = XMLParsing.getValue(element, "answer");
                return new Input(question, answer);
            } catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return null;
    }
}       